package com.ruoyi.detail.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.detail.domain.SessionDetail;
import com.ruoyi.detail.domain.SessionDetailVo;
import com.ruoyi.detail.mapper.SessionDetailMapper;
import com.ruoyi.session.mapper.SessionMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.detail.service.ISessionDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: RuoYi-Vue-master
 * @description:
 * @author: xgw
 * @create: 2025-01-07 10:24
 **/


@Service
public class SessionDetailServiceImpl implements ISessionDetailService {

    @Autowired
    private SessionDetailMapper sessionDetailMapper;

    @Autowired
    private SessionMapper sessionMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public AjaxResult selectAllSessionDetail() {
        //获取全部的session_detail
        return selectSessionDetailCommon( sessionDetailMapper.selectAllSessionDetail());
    }

    @Override
    public AjaxResult insertSessionDetail(SessionDetail sessionDetail) {
        try {
            sessionDetailMapper.insertSessionDetail(sessionDetail);
        } catch (Exception e) {
            return AjaxResult.error(sessionDetail.getLabel()+":插入聊天记录失败");
        }
       return AjaxResult.success("用户"+sessionDetail.getLabel()+":插入聊天记录成功");
    }

    @Override
    public AjaxResult updateSessionDetailById(SessionDetail sessionDetail) {
        try {
            sessionDetailMapper.updateSessionDetailById(sessionDetail);
        } catch (Exception e) {
            return AjaxResult.error(sessionDetail.getLabel()+":更新聊天记录"+sessionDetail.getId()+"失败");
        }
        return AjaxResult.success(sessionDetail.getLabel()+":更新聊天记录"+sessionDetail.getId()+"成功");
    }

    @Override
    public AjaxResult deleteSessionDetailById(Integer id) {
        //得到要删除的detail
        SessionDetail sessionDetail =  sessionDetailMapper.selectSessionDetailById(id);
        try {
            sessionDetailMapper.deleteSessionDetailById(id);
        } catch (Exception e) {
            return AjaxResult.error(sessionDetail.getLabel()+":删除聊天记录"+sessionDetail.getId()+"失败");
        }
        return AjaxResult.success(sessionDetail.getLabel()+":删除聊天记录"+sessionDetail.getId()+"成功");
    }


    @Override
    public AjaxResult dynamicSelectSessionDetail(String username, Integer sessionId, LocalDateTime startTime, LocalDateTime endTime) {
        return AjaxResult.success("查询成功",selectSessionDetailCommon(sessionDetailMapper.dynamicSelectSessionDetail(username,sessionId,startTime,endTime)));
    }

    @Override
    public AjaxResult selectSessionDetailById(Integer id) {
        return AjaxResult.success(sessionDetailMapper.selectSessionDetailById(id));
    }

    @Override
    public AjaxResult selectSessionDetailByTimestamp(Integer sessionId) {
        return selectSessionDetailCommon(sessionDetailMapper.selectSessionDetailByTimestamp(sessionId));
    }

    public  AjaxResult selectSessionDetailCommon(List<SessionDetail> list){
        //获取全部的session_detail
        List<SessionDetailVo> sdVoList = new LinkedList<>();
        //封装成为SessionDetailVo
        for (SessionDetail sessionDetail : list) {
            Integer label = sessionDetail.getLabel();
            SessionDetailVo sdVo = new SessionDetailVo();
            //给SessionDetailVo对象赋值
            sdVo.setId(sessionDetail.getId());
            sdVo.setSessionId(sessionDetail.getSessionId());
            sdVo.setLabel(label);
            sdVo.setContent(sessionDetail.getContent());
            sdVo.setSessionTitle(sessionMapper.selectSessionById(sessionDetail.getSessionId()).getTitle());
            sdVo.setTimestamp(sessionDetail.getTimestamp());
            //label = -1 机器人，label = userId > 0 用户
            if(label==-1){
                sdVo.setUsername("声语文创机器人");
                sdVoList.add(sdVo);
            }else if(label > 0){
                //转换类型
                Long aLong = Long.valueOf((long) (Integer.parseInt(label + "")));
                sdVo.setUsername(sysUserMapper.selectUserById(aLong).getUserName());
                sdVoList.add(sdVo);
            }
        }
        return AjaxResult.success(sdVoList);
    }
}
